package com.yonyou.pmclouds.messagenotice.constructor.impl;

import com.yonyou.pmclouds.basecom.billstatus.BillStatusConst;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.entity.Entity;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.messagenotice.entity.MessageNoticeVO;
import com.yonyou.pmclouds.plan.entity.PlanBillStatus;
import com.yonyou.pmclouds.plan.entity.ProjectPlanVO;
import com.yonyou.pmclouds.plan.rmiitf.ProjectPlanQuery;
import com.yonyou.pmclouds.project.entity.ProjectVO;
import com.yonyou.pmclouds.project.rmiitf.ProjectSimpleQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ProjectPlanNoticeConstructor extends AbstractMessageNoticeConstructor {

    private final ProjectPlanQuery projectPlanQuery;

    private final ProjectSimpleQuery projectSimpleQuery;

    @Autowired
    public ProjectPlanNoticeConstructor(ProjectPlanQuery projectPlanQuery, ProjectSimpleQuery projectSimpleQuery) {
        this.projectPlanQuery = projectPlanQuery;
        this.projectSimpleQuery = projectSimpleQuery;
    }


    @Override
    protected MessageNoticeVO[] generateContent(Entity vo) throws BusinessException {
        MessageNoticeVO noticeVO = coverBusinessVOToMessageVO(vo);
        if(noticeVO == null){
            return null;
        }
        // 您提交的【XX】已审批通过
        // 您提交的【XX】已被驳回，详请登录web端进行查看修改
        ProjectPlanVO projectPlanVO = projectPlanQuery.queryPlanById(noticeVO.getRelateKey());
        ProjectVO projectVO = projectSimpleQuery.queryProject(projectPlanVO.getPkProject());

        String planName = projectPlanVO.getSname();
        String prefix = getApprovalPrefixMessage(projectPlanVO.getBillstatus());
        if(prefix == null && projectPlanVO.getBillstatus() == PlanBillStatus.STATUS_EFFECTIVE) {
            prefix = APPROVAL_SUFFIX_ADOPT_MESSAGE;
        }
        if(BillStatusConst.COMMON_REJECT_STATUS == projectPlanVO.getBillstatus()) {
            prefix += "，详请登录web端进行查看修改";
        }
        if(BillStatusConst.COMMON_FREE_STATUS == projectPlanVO.getBillstatus()){
            prefix = "，需要在web端重新提交审批";
        }
        String messageContent = APPROVAL_PREFIX_MESSAGE + "[" + planName + "]" + prefix;

        noticeVO.setMessageTitle(projectVO.getSname());
        noticeVO.setMessageReceiver(projectPlanVO.getCreator());
        noticeVO.setPkTenant(projectPlanVO.getPkTenant());
        noticeVO.setMessageContent(messageContent);
        noticeVO.setRelateType(projectPlanVO.getBillType());

        return new MessageNoticeVO[]{noticeVO};
    }

    @Override
    public String getRelateType() {
        return BillTypeConst.PROCESS_PLAN_BILLTYPE;
    }
}
